# -*- coding: utf-8 -*-
from crypt import methods
from os import makedev

from flask import Flask, request, Blueprint
import sys
import time
import config
import json
#import redis
from lib.data.exts import db
from lib.data.model import UrlTask, PageResult, LogEntry, TestPageResult, TestUrlTask

from lib.crypto import check_request_sign, init_bf, check_crawler_url

app = Flask(__name__)
app.config.from_object(config)

#init_bf(config)

db.init_app(app)
with app.app_context():
    db.create_all()

bf_handle = init_bf(config)






#提交url
@app.route("/newurl",methods=['POST'])
@check_request_sign('test')
def new_task():
    data_item = json.loads(request.get_data())
    if data_item['status'] == 1 and len(data_item['data']) > 0:
        valid_num = 0
        for item in data_item['data']:
            if bf_handle.do_filter(item['url']):
                #print("%s in" % item['url'])
                new_url = UrlTask(item, int(time.time()))
                db.session.add(new_url)
                valid_num += 1
        
        new_log = LogEntry(1,len(data_item['data']),valid_num,data_item['device'], "add new url %d/%d" % (valid_num, len(data_item['data'])))
        db.session.add(new_log)
        db.session.commit()
        a = {'status': '1' ,'msg': 'ok'}
        return json.dumps(a)
    else:
        return json.dumps({'status': -1, 'msg': 'no content'})    
    
#获取任务
@app.route('/getpage', methods=['GET'])
@check_request_sign('test')
def get_page():
    cur_task = db.session.query(UrlTask).filter(UrlTask.status == 0).limit(1).first()
    if cur_task:
        tmp_res = cur_task.to_json()
        cur_task.status = 1
        db.session.commit()
        a = {'status': '1' ,'msg': 'ok', 'data': tmp_res}
        return json.dumps(a)
    else:
        a = {'status': '2', 'msg': 'no task'}
        return json.dumps(a)

#提交任务
@app.route('/submitpage',methods=['POST'])
@check_request_sign('test')
def submit_page():
    data_item = json.loads(request.get_data())
    if data_item['status'] == 1:
        #print(data_item)
        cur_item = data_item['data']
        new_page = PageResult(cur_item)
        db.session.add(new_page)
        cur_task = db.session.query(UrlTask).filter(UrlTask.id == cur_item['task_id']).first()
        cur_task.status = 2
        cur_task.finishedat = int(time.time())
        
        new_log = LogEntry(2,len(cur_item['content']),0,data_item['device'], "new page %s--%d" %  (cur_task.source, cur_task.id))
        db.session.add(new_log)
  
        db.session.commit()
    a = {'status': '1' ,'msg': 'ok'}
    return json.dumps(a)  







if __name__ == '__main__':
    app.run(debug=False,host='0.0.0.0', port=25000)
    #app.run(debug=True,host='0.0.0.0', port=38000)
